package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.EssayCourseStudyRecord;
import com.wzcl.app.dao.repository.EssayCourseStudyRecordRepository;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.service.EssayCourseStudyRecordService;
import com.wzcl.app.utils.DateUtil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;

/**
 * @author beingven
 * @Description TODO
 * @createTime 2021年01月03日 08:18:00
 */
@Service
public class EssayCourseStudyRecordServiceImpl implements EssayCourseStudyRecordService {
    @Resource
    private EssayCourseStudyRecordRepository essayCourseStudyRecordRepository;

    @Override
    @Transactional
    public RVO startStudyTask(EssayCourseStudyRecord essayCourseStudyRecord) {
        String phone = SecurityContextHolder.getContext().getAuthentication().getName();
        //查询数据库中是否已存在该任务的学习记录
        Integer count = essayCourseStudyRecordRepository.findCountByTaskIdAndPhone(essayCourseStudyRecord.getTaskId(), phone);
        if (count == 0){
            essayCourseStudyRecord.setStartTime(DateUtil.getDateBySystemTime(System.currentTimeMillis()));
            essayCourseStudyRecord.setPhone(phone);
            EssayCourseStudyRecord studyRecord = essayCourseStudyRecordRepository.save(essayCourseStudyRecord);
            return RVO.success(studyRecord);
        }
        return null;
    }

    @Override
    public RVO deleteStudyTask(String phone, Integer taskId) {
        return null;
    }

    @Override
    public RVO updateStudyTask(Date endTime, String phone, Integer taskId) {
        return null;
    }
}
